///
/// ArmyBodger 3
/// (c)2010 richyp
///
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArmyBodger.Core;

namespace ArmyBodger.UI {
    /// <summary>
    /// Create new List Form
    /// </summary>
    public partial class CreateNewListForm : Form {
        /// <summary>
        /// ArmyBookParser instance
        /// </summary>
        protected ArmyBookParser parser = null;
        /// <summary>
        /// Collection of GameSystem objects
        /// </summary>
        protected GameSystemCollection systems = new GameSystemCollection();
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="systems">Collection of loaded GameSystems</param>
        public CreateNewListForm(GameSystemCollection systems) {
            InitializeComponent();
            this.systems = systems;
            this.parser = new ArmyBookParser(systems);
            this.PopulateGameSystems();
        }
        /// <summary>
        /// Populate the drop down list of Game Systems
        /// </summary>
        public void PopulateGameSystems() {
            this.comboBoxGameSystem.Items.Clear();
            foreach (GameSystem system in this.systems) {
                this.comboBoxGameSystem.Items.Add(system);
            }

            if (this.comboBoxGameSystem.Items.Count > 0) {
                this.comboBoxGameSystem.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// Get the ArmyBook chosen by this form
        /// </summary>
        public ArmyBook SelectedArmyBook {
            get {
                return ((ArmyBookItem)this.comboBoxArmyBook.SelectedItem).ArmyBook;
            }
        }
        public string ArmyName
        {
            get { return this.Controls["nameBox"].Name; }
        }
        public int PointsLevel
        {
            get 
            {
                int points;
                if (int.TryParse(this.Controls["pointsBox"].Text, out points))
                    return points;

                return 0;
            }
        }
        #region EventHandlers
        /// <summary>
        /// The Game System was changed, repopulate ArmyBook list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxGameSystem_SelectedIndexChanged(object sender, EventArgs e) {
            this.comboBoxArmyBook.Items.Clear();
            if (this.comboBoxGameSystem.SelectedIndex != -1) {
                string armyBookPath = Properties.Settings.Default.ArmyBooksPath;
                armyBookPath += "/" + ((GameSystem)this.comboBoxGameSystem.SelectedItem).Name;
                List<string> armyBooks = new List<string>();
                armyBooks.AddRange(Directory.GetFiles(armyBookPath));
                armyBooks.AddRange(Directory.GetDirectories(armyBookPath));

                foreach (string file in armyBooks) {
                    if( file.EndsWith(".zip") || !file.Contains(".")) {
                        string path = file.Substring(file.LastIndexOf("\\"));
                        path = path.Replace("\\", "/");
                        path = ((GameSystem)this.comboBoxGameSystem.SelectedItem).Name + path;
                        ArmyBook ab = this.parser.ParseArmyBookFile(
                            Properties.Settings.Default.ArmyBooksPath,
                            path,
                            Properties.Settings.Default.TempDirectory
                            );
                        if (ab == null)
                            continue;

                        ArmyBookItem item = new ArmyBookItem(ab);
                        this.comboBoxArmyBook.Items.Add(item);
                    }
                }
                //foreach (string dir in D)
                //{
                //    string path = dir.Substring(dir.LastIndexOf("\\"));
                //    path = path.Replace("\\", "/");
                //    path = ((GameSystem)this.comboBoxGameSystem.SelectedItem).Name + path;
                //    ArmyBook ab = this.parser.ParseArmyBookFile(
                //        Properties.Settings.Default.ArmyBooksPath,
                //        path,
                //        Properties.Settings.Default.TempDirectory
                //        );
                //    ArmyBookItem item = new ArmyBookItem(ab);
                //    this.comboBoxArmyBook.Items.Add(item);
                //}
                if(this.comboBoxArmyBook.Items.Count > 0 ) {
                    this.comboBoxArmyBook.SelectedIndex = 0;
                }
            }
        }
        /// <summary>
        /// The ArmyBook was changed, show the information on the newly selected
        /// entry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxArmyBook_SelectedIndexChanged(object sender, EventArgs e) {
            this.labelAuthors.Text = this.labelVersion.Text = "";
            if (this.comboBoxArmyBook.SelectedItem != null) {
                ArmyBook ab = (ArmyBook)((ArmyBookItem)this.comboBoxArmyBook.SelectedItem).ArmyBook;
                this.labelAuthors.Text = ab.Header.Authors;
                this.labelVersion.Text = ab.Header.Version;
            }
        }
        #endregion

    }
    /// <summary>
    /// Represent an ArmyBook within the combobox
    /// </summary>
    public class ArmyBookItem {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="armyBook">The ArmyBook contained in this item</param>
        public ArmyBookItem(ArmyBook armyBook) {
            this.ArmyBook = armyBook;
        }
        /// <summary>
        /// The ArmyBook contained within this ArmyBookItem object
        /// </summary>
        public ArmyBook ArmyBook { get; set; }
        /// <summary>
        /// Overriden to display the name of the encompassed ArmyBook object
        /// </summary>
        /// <returns>The name of the ArmyBook enclosed</returns>
        public override string ToString() {
            return this.ArmyBook.Header.Name;
        }
    }
}

